// const { User } = require('../model/user')
const { code } = require('../static/secret')
const jwt = require('jsonwebtoken')
const bcrypt = require('bcrypt')
const User = require('../model/user2')
module.exports = async (ctx) => {
  const info = ctx.request.body
  const { username, password } = info
  // const user = await User.findAll({ where: { username: username } })
  const user = await User.find({ username: username })
  console.log(user)
  if (user.length !== 0) {
    ctx.body = {
      status: 403,
      message: '用户名已存在,请更换用户名重新注册',
      data: {}
    }
    return
  }
  info.password = await bcrypt.hash(password, 10)
  info.role = 'user'
  // const newUser = await User.create(info)
  const newUser = await User.create(info)
  const token = await jwt.sign(
    { username: newUser.username }, code, { expiresIn: '1h' })
  ctx.body = {
    status: 200,
    message: '用户创建成功',
    data: {
      token: token,
      role: 'user'
    }
  }
}
